// pages/02admin/12goodsShelves/goodsShelves.js
var app = getApp()
var appData = getApp().globalData
Page({
   data: {
      isShowInputLabelArr: [false, false, false, false, false, false],
      uploadedImgUrlArr: [],
      cateList: [],
      cateArr: [],
      // cateIdArr:[],
      pick_i: -1,
      goodsInfo: '',
      p: '',//坐标有x、y、w
      img_w: ''
   },

   radioChange: function (e) {
      console.log('radioChange:e', e)
   },

   onLoad: function (opt) {
      // 获取商品数据
      if (opt) {
         if (opt.goods) {
            var obj = JSON.parse(opt.goods);
            var imgArr = obj.pic_more.split(',')
            this.setData({
               goodsInfo: obj,
               isShowInputLabelArr: [true, true, true, true, true, true],
               uploadedImgUrlArr: imgArr,
            })
         }
      }

      var res = wx.getSystemInfoSync()
      this.setData({
         img_w: res.windowWidth * 0.185,
      })

      var that = this
      var param = {
         uid: appData.shopInfo.userid,
         is_ad: 1 // 是否后台添加商品用分类,为1时，不请求固定分类“酒水”
      }
      getApp().getJsonData('api/space/product_cate.php', param, function (cateData) {
         that.handleCateData(cateData)
      })
   },


   handleCateData: function (res) {
      if (res.code == 0) {
         var rd = res.data
         var cate_arr = []
         //var cate_id_arr = []
         for (var i = 0; i < rd.length; i++) {
            var c = rd[i]
            cate_arr.push(c.name)
            //cate_id_arr.push(c.cateId)
         }
         var p_i = this.getIndex(rd)
         console.log('==========p_i', p_i)
         this.setData({//页面用分类数据
            cateList: rd,
            cateArr: cate_arr,
            //cateIdArr: cate_id_arr,
            pick_i: p_i,
         })
         console.log('==========this.data', this.data)
      }
   },

   getIndex: function (rd) {
      for (var i = 0; i < rd.length; i++) {
         if (rd[i].cateId == this.data.goodsInfo.custom_cat_id) {
            return i
         }
      }
      return -1
   },

   pickerClick: function (e) {
      console.log(e);
      console.log('pickerClick:e');
      this.setData({ pick_i: e.detail.value })
   },

   chooseImg: function (e) {
      var t = this
      t.setData({ p: e.detail })
      console.log('【找坐标】', t.data.p)
      wx.chooseImage({
         count: 1, // 默认9
         sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图，默认二者都有
         sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有
         success: function (res) {
            t.uploadImg(res.tempFilePaths)
         }
      })
   },

   uploadImg: function (filePath) {
      var that = this
      wx.uploadFile({
         url: 'https://qapi.openally.com/api_qzg/api/admin/upload2.php',
         filePath: filePath[0],
         name: 'file',
         header: { 'content-type': 'application/json' },
         formData: { 'user': 'test' },
         success: function (res) {
            console.log('【uploadImg: success: res】', res);
            var obj = JSON.parse(res.data)
            console.log('【uploadImg: success: obj】', obj);
            that.previewUploadedImg(obj);
         },
         fail: function (res) {
            console.log('uploadImg: fail: res', res);
         }
      })
   },

   delImg: function (e) {
      var i = e.currentTarget.id
      console.log('this.data', this.data)
      var arr = this.data.uploadedImgUrlArr
      arr.splice(i, 1)
      this.setData({
         uploadedImgUrlArr: arr
      })
   },

   previewUploadedImg: function (obj) {
      var arr = this.data.uploadedImgUrlArr
      arr.push(obj.data)
      this.setData({
         uploadedImgUrlArr: arr,
      })
      console.log(this.data.uploadedImgUrlArr);
      console.log('this.data.uploadedImgUrlArruploadedImgUrlArr');
   },

   submitGoodsInfo: function (e) {
      var t = this
      var td = this.data
      var params = e.detail.value

      console.log('submitGoodsInfo:params', params)
      params.member_id = appData.shopInfo.userid
      if (td.pick_i != -1) {
         params.custom_cat_id = td.cateList[td.pick_i].cateId
      }
      var imgArr = td.uploadedImgUrlArr
      params.pic = imgArr[0]
      params.pic_more = ''
      for (var i = 0; i < imgArr.length; i++) {
         params.pic_more += imgArr[i]
         if (i < imgArr.length - 1) {
            params.pic_more += ','
         }
      }

      if (t.data.goodsInfo == '') {
         app.getJsonData('api/admin/product_add.php', params, function (resData) {
            wx.navigateBack();
         })
      } else {
         params.id = t.data.goodsInfo.id
         app.getNetData('product/mg/edit', params, function (res) {
            app.noticeGoodsList(res.data)
            wx.navigateBack();
         })
      }
   },

   showInputLabel: function (e) {
      if (e.detail.value.length > 1) return;
      var index = e.currentTarget.id;
      var isShowArray = this.data.isShowInputLabelArr;
      isShowArray[index] = true;

      this.setData({
         isShowInputLabelArr: isShowArray
      })
   },
})